﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.FuelCostCommand;
using T.Basic.Read.API.Appliction.Command.LineCommand;
using T.Basic.Repository.BasicInterface;

namespace T.Basic.Read.API.Controllers
{
    /// <summary>
    /// 油费管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//JWT验证
    [ApiController]
    public class FuelCostsController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        private readonly IFuelCostRepository _fuelCostRepository;
        public FuelCostsController(IMediator mediator,IFuelCostRepository fuelCostRepository)
        {
            _mediator = mediator;
            _fuelCostRepository= fuelCostRepository;
        }
        /// <summary>
        /// 显示经办人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<FuelCost>> GetFuelCostGetHandled([FromQuery]FuelCostGetHandledCommand command)
        {
            try
            {

                return await _mediator.Send(command);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示油费信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<FuelCost>>> GetLinesAll([FromQuery] FuelCostListCommand command)
        {
            ApiResult<PageDto<FuelCost>> result = new ApiResult<PageDto<FuelCost>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                result.Data =await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 导出油费文档
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<IActionResult> GetExcelName([FromQuery] FuelCostListCommand command)
        {
            try
            {
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("油费列表");
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("编号");
                row.CreateCell(1).SetCellValue("车牌号");
                row.CreateCell(2).SetCellValue("加油费用/元");
                row.CreateCell(3).SetCellValue("起始公里数");
                row.CreateCell(4).SetCellValue("支付方式");
                row.CreateCell(5).SetCellValue("经办人");
                row.CreateCell(6).SetCellValue("创建人");
                row.CreateCell(7).SetCellValue("创建时间");
                var fueList = await _mediator.Send(command);
                int index = 1;
                foreach (var item in fueList.list)
                {
                    row = sheet.CreateRow(index);
                    row.CreateCell(0).SetCellValue(item.FuelCostId);
                    row.CreateCell(1).SetCellValue(item.LicensePlateNumber);
                    row.CreateCell(2).SetCellValue((double)item.FuelCostPerLiter);
                    row.CreateCell(3).SetCellValue((double)item.StartingKilometers);
                    row.CreateCell(4).SetCellValue(item.PayMethod==1?"微信支付": item.PayMethod == 2?"支付宝支付": item.PayMethod==3?"银联支付":item.PayMethod==4?"企业转账": item.PayMethod==5?"线下支付": item.PayMethod==6?item.OtherPayMethod:"");
                    row.CreateCell(5).SetCellValue(item.HandledBy);
                    row.CreateCell(6).SetCellValue(item.CreatedBy);
                    row.CreateCell(7).SetCellValue(item.CreatedDate.ToString());
                    index++;
                }

                //导出
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                string excelMine = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, excelMine, "油费列表.xlsx");

            }
            catch (Exception)
            {

                throw;
            }
           
           
        }
    }
}
